from fastapi import FastAPI, Request
from fastapi.middleware.cors import CORSMiddleware
from fastapi.templating import Jinja2Templates
import uvicorn

# 创建app对象
app = FastAPI()
# 创建模板
templates = Jinja2Templates(directory="templates")
# 请求源列表
origins = ["http://localhost", "http://localhost:8000", "http://127.0.0.1:8000/"]

# 添加CORS中间件
app.add_middleware(
    CORSMiddleware,
    # allow_origins=origins,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)


# 路由函数
@app.get("/")
async def home(request: Request):
    return templates.TemplateResponse("index.html", {"request": request})


# 路由函数
@app.get("/get_data")
async def get_data(request: Request):
    return {"Hello":"World."}



if __name__ == "__main__":
    uvicorn.run("main:app", host="127.0.0.1", port=8000, reload=True)
